package org.igeek.atomqq.task;

import java.net.URL;
import java.net.URLEncoder;

import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.net.HttpConnection;
import org.igeek.atomqq.net.HttpConnection.Request_TYPE;
import org.igeek.atomqq.net.Response;
import org.igeek.atomqq.util.URLManager;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


/**
 * 发送消息
 * 正常返回类型: application/json
 * 
 * POST application/json	http://d.web2.qq.com/channel/send_buddy_msg2
 * @author hangxin1940@gmail.com
 * @version 创建时间：2012-1-29 下午1:32:26
 * 
 */
public class SendFriendMessageTask extends CachedAsyncTask<Void,Void,Void>{
	public static final int ERROR=0x5029;
	
	private static long msgid=32340000;
	
	private TaskResultListener listener;
	private SendFriendMessageResponse response;
	private long uin;
	private int flag;
	private String message;
	
	private Handler handler=new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ERROR:
				activity.showDialog(ERROR, msg.getData());
				break;
			default:
				activity.showDialog(BaseActivity.DIALOG_ERROR, msg.getData());
				break;
			}
		}
	};
	
	private BaseActivity activity;
	
	public SendFriendMessageTask(BaseActivity activity){
		this.activity=activity;
	}
	
	public void setOnTaskResult(TaskResultListener listener,int flag){
		this.listener=listener;
		this.flag=flag;
	}
	
	public void setParams(long uin,String message){
		this.uin=uin;
		this.message=message;
	}

	@Override
	protected Void doInBackground(Void... params) {
		
		try {
			//JSONObject js=createJson(uin, message);
			
			String psessionid=HttpConnection.getCookieManager().getCookieValue("psessionid");
			String clientid=HttpConnection.getCookieManager().getCookieValue("clientid");
			
			String js="%7B%22to%22%3A"+uin+"%2C%22face%22%3A627%2C%22content%22%3A%22%5B%5C%22"+
			URLEncoder.encode(message,"utf-8")
			+"%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A%5C%2210%5C%22%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22msg_id%22%3A"+(++msgid)+"%2C%22clientid%22%3A%22"+clientid+"%22%2C%22psessionid%22%3A%22"+psessionid+"%22%7D";
			
			StringBuffer surl=new StringBuffer(URLManager.SEND_FRIEND_MSG);
			surl.append("?r=");
			surl.append(js);
			surl.append("&clientid=");
			surl.append(clientid);
			surl.append("&psessionid=");
			surl.append(psessionid);
			
			
			URL url = new URL(surl.toString());
			
			HttpConnection conn=new HttpConnection(url, Request_TYPE.POST);
			conn.setReferer(URLManager.REFER_d_web2_qq_com_proxy);
			conn.connect();
			Response cesponses=conn.getResponse();
			conn.disconnect();
			
			response=new SendFriendMessageResponse();
			
			JSONObject r=cesponses.getJsonObj();
			String result=r.getString("result");
			
			boolean success=false;
			
			if("ok".equals(result))
				success=true;
			
			
			response.setSuccsess(success);
			
			
		}catch (Exception e) {
			e.printStackTrace();
			showErrorDialog("网络异常",ERROR);
		}
		
		
		return null;
	}
	
	private JSONObject createJson(long uin,String message) throws JSONException{
		String psessionid=HttpConnection.getCookieManager().getCookieValue("psessionid");
		String clientid=HttpConnection.getCookieManager().getCookieValue("clientid");
		
		JSONObject msg=new JSONObject();
		msg.put("to", uin);
		msg.put("face", 627);
		
		StringBuffer content=new StringBuffer();
		content.append("[\"");
		content.append(message);
		content.append("\",[\"font\",{name\":\"宋体\",\"size\":\"10\",\"style\":[0,0,0],\"color\":\"000000\"}]]");
		
		msg.put("content", content.toString());
		
		msg.put("msg_id", ++msgid);
		msg.put("clientid", clientid);
		msg.put("psessionid", psessionid);
		return msg;
	}

	
	private void showErrorDialog(String message,int flag){
		Bundle bundle=new Bundle();
		bundle.putString(BaseActivity.FLAG_ERROR, message);
		Message msg=new Message();
		msg.setData(bundle);
		msg.what=flag;
		handler.sendMessage(msg );
	}
	
	@Override
	protected void onPostExecute(Void result) {
		if(null!=response)
			listener.onTaskResult(response,flag);
	}	

	
	public class SendFriendMessageResponse extends Response{
		private boolean succsess;

		public void setSuccsess(boolean succsess) {
			this.succsess = succsess;
		}

		public boolean getSuccsess() {
			return succsess;
		}
		
		
	}
}
